const express = require("express");
const multer = require("multer");
const { UploadError } = require("../utils/errors");
const { uploading, formatResponse } = require("../utils/tool");
const router = express.Router();

// 获取上传图片数据
router.post("/", async function (req, res, next) {
  // single方法里面写上传控件的name值
  uploading.single("file")(req, res, function(err) {
    if(err instanceof multer.MulterError) {
      next(new UploadError("上传文件失败，请检查文件的大小，控制在2MB以内"));
    } else {
      const path = "/static/uploads/" + req.file.filename;
      res.send(formatResponse(0, "", path))
    }
  })
})

module.exports = router;